package com.example.day12;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * 多布局
 * <p>
 * banner
 * 列表
 */
public class HomeAdapter extends RecyclerView.Adapter {

    public static final int TYPE_1 = 1;
    public static final int TYPE_2 = 2;
    List<HomeBean.DataBean.DatasBean> dataBeans = new ArrayList<>();
    List<BannerBean.DataBean> bannerList = new ArrayList<>();

    /**
     * 接收列表的数据集合
     * @param dataBeans
     */
    public void resetDataList(   List<HomeBean.DataBean.DatasBean> dataBeans  ){

        this.dataBeans.clear();
        this.dataBeans.addAll(dataBeans);
        notifyDataSetChanged();

    }

    public void loadMore(   List<HomeBean.DataBean.DatasBean> dataBeans  ){

        this.dataBeans.addAll(dataBeans);
        notifyDataSetChanged();

    }
    /**
     * 接收banner的集合
     * @param bannerList
     */
    public void addBannerList( List<BannerBean.DataBean> bannerList ){

        this.bannerList.addAll(bannerList);
        notifyDataSetChanged();

    }
    Context context;

    public HomeAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int type) {
        switch (type){
            case TYPE_1:
                return new ViewHolder1(LayoutInflater.from(context).inflate(R.layout.banner_layout,viewGroup,false));
            case TYPE_2:
                return new ViewHolder2(LayoutInflater.from(context).inflate(R.layout.list_layout,viewGroup,false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int pos) {

        int type = getItemViewType(pos);

        switch (type){
            case TYPE_1:
                ViewHolder1 viewHolder1 = (ViewHolder1) viewHolder;
                viewHolder1.banner.setImages(bannerList).setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object path, ImageView imageView) {
                        // 展示每张图片 glide

                        BannerBean.DataBean dataBean = (BannerBean.DataBean) path;

                        Glide.with(context).load(dataBean.getImagePath()).into(imageView);
                    }
                    // 切记  开启任务
                }).start();

                break;

            case TYPE_2:

                if (bannerList.size()>0){
                    pos = pos-1;
                }
                //获取数据
                final HomeBean.DataBean.DatasBean datasBean = dataBeans.get(pos);

                ViewHolder2 viewHolder2 = (ViewHolder2) viewHolder;
                viewHolder2.title.setText(datasBean.getTitle());

                viewHolder2.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (clickItem!=null){
                            clickItem.click(datasBean);
                        }
                    }
                });
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 && bannerList.size() > 0) {
            return TYPE_1;
        } else {
            return TYPE_2;
        }

    }

    @Override
    public int getItemCount() {
//        if (bannerList.size()>0){
//            return dataBeans.size()+1;
//        }else{
//            return dataBeans.size();
//        }
        return bannerList.size()>0 ? dataBeans.size()+1:dataBeans.size();
    }

    class ViewHolder1 extends RecyclerView.ViewHolder{

        Banner banner;
        public ViewHolder1(@NonNull View itemView) {
            super(itemView);
            banner = itemView.findViewById(R.id.banner);
        }
    }

    class ViewHolder2 extends RecyclerView.ViewHolder{

     TextView title;
        public ViewHolder2(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
        }
    }

    ClickItem clickItem;

    public void setClickItem(ClickItem clickItem) {
        this.clickItem = clickItem;
    }

    interface ClickItem{

        void click(HomeBean.DataBean.DatasBean datasBean  );
    }
}
